#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: dict.py
# Description: 
#   Read or write a python dict to/from a text file.
#   Read a File Into a Dictionary.
# Edit History: 
#   2011-10-18    File created.
#===============================================================================
def writeDict(filename, dict):
    f = open(filename,'w')
    f.write(str(dict))

def readDict(filename):
    f = open(filename,'r')
    my_dict = eval(f.read())
    return my_dict

def readFile(fileHandle, dictName): 
    line = fileHandle.readline() 
    dictName = {} 
    keycounter = 1 
    while line: 
        key = str(keycounter) 
        dictName[key] = line 
        keycounter = keycounter + 1 
        line = fileHandle.readline() 

    return dictName

def main():
    writeDict('test', {'a': 1, 'b': 2})
    print(readDict('test'))

    print(readFile(open('dict.py'), {}))

if __name__=='__main__':
    main()
